﻿/*
 *Author: Jeff Liu 
 *
 *Under MIT License
 *jebberwocky@gmail.com
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CtpGglMap.Impl;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace CtpGglMap.Plus
{
    /// <summary>
    /// Subclass of StaticGMap. For features which not been included in original Google API
    /// </summary>
    public class StaticGMapPlus: StaticGMap
    {
        /// <summary>
        /// Fetch image data from Google service, then manipulate the fetched data as image 
        /// </summary>
        /// <returns>Bitmap</returns>
        public Bitmap FetchBitmap()
        {
            using (Stream imageSteam = this.Fetch())
            {
                if (imageSteam != null)
                {
                    using (System.Drawing.Image _Image = System.Drawing.Image.FromStream(imageSteam))
                    {
                        Bitmap bmp = new Bitmap(_Image.Width, _Image.Height, PixelFormat.Format32bppArgb);
                        using (Graphics g = Graphics.FromImage(bmp))
                        {
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                            g.DrawImage(_Image, 0, 0);
                            if (this.GetItems() != null && this.GetItems().Count > 0)
                            {
                                foreach (IMapItem mi in this.GetItems())
                                {
                                    if (mi != null && mi.GetType() == typeof(GMarkerPlus))
                                    {
                                        GMarkerPlus marker = mi as GMarkerPlus;
                                        g.DrawString(marker.Title, 
                                            marker.TitleFont, 
                                            marker.TitleBrush, 
                                            marker.GetPointF(g));
                                    }
                                }
                            }
                            return bmp;
                        }            
                    }
                }
            }
            return null; 
        }
    }
}
